#include "systray.h"

CSysTray::CSysTray(QWidget *parent, QIcon &trayIcon)
	: QObject(parent)
{
	m_pWgtParent = parent;

	m_pSysTrayIcon = new QSystemTrayIcon(trayIcon, parent);
	m_pMenu = new QMenu(parent);

	QAction *m_pAct = m_pMenu->addAction(tr("显示窗口"));
	QObject::connect(m_pAct, SIGNAL(triggered()), parent, SLOT(show()));

	m_pAct = m_pMenu->addAction(tr("隐藏窗口"));
	QObject::connect(m_pAct, SIGNAL(triggered()), parent, SLOT(hide()));

	m_pMenu->addSeparator();

	m_pAct = m_pMenu->addAction(tr("关于..."));
	QObject::connect(m_pAct, SIGNAL(triggered()), parent, SLOT(showAbout()));

	m_pMenu->addSeparator();

	m_pAct = m_pMenu->addAction(tr("退出"));
	QObject::connect(m_pAct, SIGNAL(triggered()), parent, SLOT(closeMainWidget()));

	// Sys Tray 
	m_pSysTrayIcon->setContextMenu(m_pMenu);
	QObject::connect(m_pSysTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(activeSysTray(QSystemTrayIcon::ActivationReason)));
}

CSysTray::~CSysTray()
{
	if (m_pSysTrayIcon)
	{
		delete m_pSysTrayIcon;
		m_pSysTrayIcon = 0;
	}

	if (m_pMenu)
	{
		delete m_pMenu;
		m_pMenu = 0;
	}
}

void CSysTray::activeSysTray(QSystemTrayIcon::ActivationReason reason)
{
	if (QSystemTrayIcon::Trigger == reason)
	{
		m_pWgtParent->show();
	}
	else if (QSystemTrayIcon::Context == reason)
	{
		m_pMenu->show();
	}
/*	switch(reason)
	{
	case QSystemTrayIcon::Trigger:
		
		break;
	case QSystemTrayIcon::DoubleClick:
		
		break;
	case QSystemTrayIcon::MiddleClick:
		break;
	case QSystemTrayIcon::Context:		
		break;
	default:
		;
	}*/
}

void CSysTray::showSysTray()
{
	m_pSysTrayIcon->show();
}

bool CSysTray::isVisible()
{
	return m_pSysTrayIcon->isVisible();
}

void CSysTray::hideSysTray()
{
	m_pSysTrayIcon->hide();
}
